/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author java
 */
@Entity
@Table(name = "SALE_LINES")
@NamedQueries({
    @NamedQuery(name = "SaleLines.findAll", query = "SELECT s FROM SaleLine s"),
    @NamedQuery(name = "SaleLines.findById", query = "SELECT s FROM SaleLine s WHERE s.id = :id"),
    @NamedQuery(name = "SaleLines.findByViewNo", query = "SELECT s FROM SaleLine s WHERE s.viewNo = :viewNo"),
    @NamedQuery(name = "SaleLines.findByItemPrice", query = "SELECT s FROM SaleLine s WHERE s.itemPrice = :itemPrice"),
    @NamedQuery(name = "SaleLines.findByQuantity", query = "SELECT s FROM SaleLine s WHERE s.quantity = :quantity"),
    @NamedQuery(name = "SaleLines.findByNotes", query = "SELECT s FROM SaleLine s WHERE s.notes = :notes"),
    @NamedQuery(name = "SaleLines.findByLastPaidYear", query = "SELECT s FROM SaleLine s WHERE s.lastPaidYear = :lastPaidYear"),
    @NamedQuery(name = "SaleLines.findByDiscount", query = "SELECT s FROM SaleLine s WHERE s.discount = :discount"),
    @NamedQuery(name = "SaleLines.findByP", query = "SELECT s FROM SaleLine s WHERE s.p = :p")})

public class SaleLine implements Serializable {
	
    private static final long serialVersionUID = 1L;
    
    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    
    @Column(name = "VIEW_NO")
    private Integer viewNo;
    
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "ITEM_PRICE")
    private Double itemPrice;
    
    @Column(name = "QUANTITY")
    private Double quantity;
    
    @Size(max = 50)
    @Column(name = "NOTES")
    private String notes;
    
    @Column(name = "LAST_PAID_YEAR")
    private Integer lastPaidYear;
    
    @Column(name = "DISCOUNT")
    private Double discount;
    
    @Column(name = "p")
    private Boolean p;
    
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "pl")
    private Double pl; // is a copy of the the  field ITEM.ITEM_DEF_END_USER_PRICE
    
    @JoinColumn(name = "SALE_ID", referencedColumnName = "ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Sale saleId;
    
    @JoinColumn(name = "LINE_ID", referencedColumnName = "ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Line lineId;
    
    @JoinColumn(name = "ITEM_ID", referencedColumnName = "ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Item itemId;

    @Transient
    private double netValue ;

    @Transient
    private String barcode ;

    
    public double getNetValue() {
		return netValue;
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public void setNetValue(double netValue) {
		this.netValue = netValue;
	}

	public SaleLine() {
		this.setDiscount(0d);
		this.setItemPrice(0d);
    }

    public SaleLine(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getViewNo() {
        return viewNo;
    }

    public void setViewNo(Integer viewNo) {
        this.viewNo = viewNo;
    }

    public Double getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(Double itemPrice) {
        this.itemPrice = itemPrice;
    }

    public Double getQuantity() {
        return quantity;
    }

    public void setQuantity(Double quantity) {
        this.quantity = quantity;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public Integer getLastPaidYear() {
        return lastPaidYear;
    }

    public void setLastPaidYear(Integer lastPaidYear) {
        this.lastPaidYear = lastPaidYear;
    }

    public Double getDiscount() {
        return discount;
    }

    public void setDiscount(Double discount) {
        this.discount = discount;
    }

    public Boolean getP() {
        return p;
    }

    public void setP(Boolean p) {
        this.p = p;
    }

    public Sale getSaleId() {
        return saleId;
    }

    public void setSaleId(Sale saleId) {
        this.saleId = saleId;
    }

    public Line getLineId() {
        return lineId;
    }

    public void setLineId(Line lineId) {
        this.lineId = lineId;
    }

    public Item getItemId() {
        return itemId;
    }

    public void setItemId(Item itemId) {
        this.itemId = itemId;
    }

    public Integer getBusinessPrimaryKey()
    {
        //return  (viewNo==null?"":viewNo.toString()) + ((itemId==null)?"":itemId.getId()) + (quantity==null?"":quantity.toString())  ;
    	return new Integer((viewNo==null?"":viewNo) +""+ (itemId==null?"":itemId.getId())) ;
    }
    
    
    public Double getPl() {
	   if(itemId!=null)
	   {
		   return itemId.getItemDefEndUserPrice();
	   }
    	return pl;
	}

	public void setPl(Double pl) {
		this.pl = pl;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
    public int hashCode() {
        int hash = 0;
        
        hash += (getBusinessPrimaryKey() != null ? getBusinessPrimaryKey().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SaleLine)) {
            return false;
        }
        SaleLine other = (SaleLine) object;
        if ((this.getBusinessPrimaryKey() == null && other.getBusinessPrimaryKey() != null) 
        		|| (this.getBusinessPrimaryKey() != null 
        		&& !this.getBusinessPrimaryKey().equals(other.getBusinessPrimaryKey()))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.SaleLines[ id=" + id + " ]";
    	//return getBusinessPrimaryKey();
    }
    
}
